home *** CD-ROM | disk | FTP | other *** search
- Path: tech.cftnet.com!not-for-mail
- From: wcowley@cftnet.com (Wes Cowley)
- Newsgroups: comp.lang.c++
- Subject: Re: Does this create memory leak?...
- Date: 17 Feb 1996 00:18:53 GMT
- Organization: CFTnet
- Message-ID: <4g36te$fvs@tech.cftnet.com>
- References: <3123DF68.1677@sierra.net>
- NNTP-Posting-Host: ppp251_6.cftnet.com
- X-Newsreader: TIN [UNIX 1.3 950824BETA PL0]
-
- T Colwell (snowbull@sierra.net) wrote:
-
- : CAT::CAT()
- : {
- : itsAge = new int;
- : itsWeight = new int;
- : *itsAge = 5;
- : *itsWeight = 9;
- : }
- :
- : CAT CAT::operator=(const CAT & rhs)
- : {
- : if (this == &rhs)
- : return *this;
- : itsAge = new int;
- : itsWeight = new int;
- : *itsAge = rhs.GetAge();
- : *itsWeight = rhs.GetWeight();
- : }
- :
- : The assignment operator reinitializes itsAge and itsWeight to
- : point to new heap addresses,
- : what happens to the addresses they were pointing to before?
- : Doesn't this create stranded addresses (leaks)?
-
- Yep. You need to delete the previous pointers in your assignment
- operator before you overwrite them.
-
- Wes
-